﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using Meqs.Entity.Views;
using Meqs.Logic;
using Meqs.Logic.Interface;
using Meqs.Utilities;

public partial class Credit_Teacher_Reprots_GoodClass : ContainerBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        InitGradeSelect();
    }

    /// <summary>
    /// 初始化年级选择下拉框
    /// </summary>
    private void InitGradeSelect()
    {
        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", SchoolMark);
        IList<SchoolInfo> schoolList = Container<ISchoolInfoLogic>.Instance.FindAll(ht);
        switch (schoolList[0].SchoolOpt)
        {
            case 1:
                selGrade.Items.Add(new ListItem("一年级", "1"));
                selGrade.Items.Add(new ListItem("二年级", "2"));
                selGrade.Items.Add(new ListItem("三年级", "3"));
                selGrade.Items.Add(new ListItem("四年级", "4"));
                selGrade.Items.Add(new ListItem("五年级", "5"));
                selGrade.Items.Add(new ListItem("六年级", "6"));
                break;
            case 2:
                selGrade.Items.Add(new ListItem("初一", "7"));
                selGrade.Items.Add(new ListItem("初二", "8"));
                selGrade.Items.Add(new ListItem("初三", "9"));
                break;
            case 3:
                selGrade.Items.Add(new ListItem("高一", "10"));
                selGrade.Items.Add(new ListItem("高二", "11"));
                selGrade.Items.Add(new ListItem("高三", "12"));
                break;
            case 4:
                selGrade.Items.Add(new ListItem("一年级", "1"));
                selGrade.Items.Add(new ListItem("二年级", "2"));
                selGrade.Items.Add(new ListItem("三年级", "3"));
                selGrade.Items.Add(new ListItem("四年级", "4"));
                selGrade.Items.Add(new ListItem("五年级", "5"));
                selGrade.Items.Add(new ListItem("六年级", "6"));
                selGrade.Items.Add(new ListItem("初一", "7"));
                selGrade.Items.Add(new ListItem("初二", "8"));
                selGrade.Items.Add(new ListItem("初三", "9"));
                break;
            case 5:
                selGrade.Items.Add(new ListItem("初一", "7"));
                selGrade.Items.Add(new ListItem("初二", "8"));
                selGrade.Items.Add(new ListItem("初三", "9"));
                selGrade.Items.Add(new ListItem("高一", "10"));
                selGrade.Items.Add(new ListItem("高二", "11"));
                selGrade.Items.Add(new ListItem("高三", "12"));
                break;
        }
    }

    protected void btnFlash_Click(object sender, EventArgs e)
    {
        int schoolYear = 0;
        int term = 0;
        int year = 0;
        int month = 0;
        int grade = 0;
        if (!Int32.TryParse(txtSchoolYear.Text, out schoolYear))
            return;
        if (!Int32.TryParse(txtTerm.Text, out term))
            return;
        if (!Int32.TryParse(txtYear.Text, out year))
            return;
        if (!Int32.TryParse(txtMonth.Text, out month))
            return;
        if (!Int32.TryParse(selGrade.Text, out grade))
            return;

        DateTime startTime = new DateTime(year, month, 1);
        IList<GoodCadreView> resultList = Container<IClassEvalTypeCatchLogic>.Instance.FindGoodClass(SchoolMark, schoolYear, term, startTime, startTime.AddMonths(1), grade);

        CreateTable(resultList);
    }

    private void CreateTable(IList<GoodCadreView> resultList)
    {
        //ListSort<GoodCadreView> sort = new ListSort<GoodCadreView>(resultList, "ClassID", true);
        //resultList = sort.Sort();

        Dictionary<int, String> typePair = new Dictionary<int, string>();
        foreach (GoodCadreView view in resultList)
        {
            if (!typePair.ContainsKey(view.TypeID))
                typePair.Add(view.TypeID, view.TypeName);
        }

        int width = 0;
        #region 构造表头
        TableHeaderRow hrow = new TableHeaderRow();
        TableHeaderCell hcell = new TableHeaderCell();
        hcell.Text = "班级";
        hcell.Style[HtmlTextWriterStyle.Width] = "100px";
        width += 100;
        hrow.Cells.Add(hcell);

        foreach (KeyValuePair<int, string> pair in typePair)
        {
            hcell = new TableHeaderCell();
            hcell.Text = pair.Value;
            hcell.Style[HtmlTextWriterStyle.Width] = "120px";
            width += 120;
            hrow.Cells.Add(hcell);
        }

        hcell = new TableHeaderCell();
        hcell.Text = "总分";
        hcell.Style[HtmlTextWriterStyle.Width] = "70px";
        width += 70;
        hrow.Cells.Add(hcell);

        tbResult.Rows.Add(hrow);
        tbResult.Style[HtmlTextWriterStyle.Width] = String.Format("{0}px", width);
        #endregion

        if (resultList.Count < 1)
            return;

        #region 构造内容
        TableRow row = new TableRow();
        TableCell cell;
        int oldID = -1;
        double sumScore = 0;
        Dictionary<int, double> scoreList = new Dictionary<int, double>();
        foreach (GoodCadreView view in resultList)
        {
            if (view.ClassID != oldID)
            { // 新行
                if (oldID != -1)
                { // 非第一行
                    sumScore = 0;
                    foreach (KeyValuePair<int, string> pair in typePair)
                    {
                        cell = new TableCell();
                        if (scoreList.ContainsKey(pair.Key))
                        {
                            cell.Text = scoreList[pair.Key].ToString();
                            sumScore += scoreList[pair.Key];
                        }
                        row.Cells.Add(cell);
                    }
                    cell = new TableCell();
                    cell.Text = sumScore.ToString();
                    row.Cells.Add(cell);
                    tbResult.Rows.Add(row);
                }
                row = new TableRow();
                cell = new TableCell();
                cell.Text = view.ClassName;
                row.Cells.Add(cell);
                scoreList = new Dictionary<int, double>();
            }
            if (!scoreList.ContainsKey(view.TypeID))
                scoreList.Add(view.TypeID, view.Score);
            else
                scoreList[view.TypeID] += view.Score;
            oldID = view.ClassID;
        }
        // 计算最后一行
        sumScore = 0;
        foreach (KeyValuePair<int, string> pair in typePair)
        {
            cell = new TableCell();
            if (scoreList.ContainsKey(pair.Key))
            {
                cell.Text = scoreList[pair.Key].ToString();
                sumScore += scoreList[pair.Key];
            }
            row.Cells.Add(cell);
        }
        cell = new TableCell();
        cell.Text = sumScore.ToString();
        row.Cells.Add(cell);
        tbResult.Rows.Add(row);
        #endregion
    }

    [WebMethod]
    public static string GetExcel(ExcelTableCell[][] cells)
    {
        string temp_path = HttpContext.Current.Server.MapPath("../../../");
        string save_path = temp_path + "download\\" + DateTime.Now.Ticks.ToString() + "_profile.xls";
        HttpContext.Current.Session["filepath"] = save_path;
        ExcelUtil.ExcelExport(save_path, DateTime.Now.Ticks.ToString() + "优秀班级档案", 1, 0, cells);
        return "";
    }
}
